/*
 * $Id$
 *
 * Copyright 2008 HuangHuang. All rights reserved.
 */

package com.crm.web.page;

import org.apache.tapestry.html.BasePage;
import org.apache.tapestry.form.IPropertySelectionModel;
import org.apache.tapestry.form.StringPropertySelectionModel;
import org.apache.tapestry.valid.ValidationConstraint;
import org.apache.tapestry.valid.ValidationDelegate;
import org.apache.tapestry.annotations.InjectState;
import org.apache.tapestry.annotations.Bean;
import org.apache.tapestry.annotations.Persist;
import org.apache.tapestry.annotations.InjectPage;
import org.apache.tapestry.event.PageBeginRenderListener;
import org.apache.tapestry.event.PageEvent;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.Date;

import com.crm.web.util.Tools;
import com.crm.web.util.Constants;
import com.crm.service.IProcessService;
import com.crm.model.Process;
import com.crm.model.User;
import com.crm.model.Contact;
import com.javaforge.tapestry.spring.annotations.InjectSpring;

/**
 * @author <a href="mailto:huang0510@hotmail.com">HuangHuang</a>
 * @version $Id$
 *          在繁忙中寻找心灵的宁静
 */
public abstract class ProcessNew extends BasePage implements PageBeginRenderListener {

    private static Log log = LogFactory.getLog(ProcessNew.class);

    public abstract Date getCommuneTime();

    public abstract String getConclusionSelected();

    public abstract String getCommuneWaySelected();

    public abstract String getCommuneResult();

    public abstract String getComment();

    @InjectSpring("processService")
    public abstract IProcessService getProcessService();

    @InjectState("userSession")
    public abstract User getUserSession();

    @InjectPage("ProcessInfo")
    public abstract ProcessInfo getProcessInfo();

    @Bean
    public abstract ValidationDelegate getDelegate();

    @Persist
    public abstract Contact getContactInfo();

    public abstract void setContactInfo(Contact contactInfo);

    public String getSerialNum() {
        return getProcessService().getNewSerialNum(getContactInfo().getId());
    }

    public IPropertySelectionModel getConclusionSelectionModel() {
        return new StringPropertySelectionModel(Constants.REMIND_DAYS);
    }

    public IPropertySelectionModel getCommuneWaySelectionModel() {
        return new StringPropertySelectionModel(Constants.COMMUNE_WAY);
    }

    public IPropertySelectionModel getCommuneResultModel() {
        return new StringPropertySelectionModel(Constants.COMMUNE_RESULT);
    }

    public Integer translate(String selected) {
        if (selected.equals("1天")) return 1;
        else if (selected.equals("2天")) return 2;
        else if (selected.equals("3天")) return 3;
        else if (selected.equals("4天")) return 4;
        else if (selected.equals("5天")) return 5;
        else if (selected.equals("6天")) return 6;
        else if (selected.equals("7天")) return 7;
        else if (selected.equals("8天")) return 8;
        else if (selected.equals("9天")) return 9;
        else if (selected.equals("10天")) return 10;
        else if (selected.equals("11天")) return 11;
        else if (selected.equals("12天")) return 12;
        else if (selected.equals("13天")) return 13;
        else if (selected.equals("14天")) return 14;
        else if (selected.equals("15天")) return 15;
        else if (selected.equals("20天")) return 20;
        else if (selected.equals("25天")) return 25;
        else if (selected.equals("30天")) return 30;
        else if (selected.equals("40天")) return 40;
        else if (selected.equals("50天")) return 50;
        else if (selected.equals("60天")) return 60;
        else return 0;
    }

    /**
     * listener method of form submit
     *
     * @return page string.
     */
    public String infoSubmit() {
        try {
            if (getDelegate().getHasErrors()) {
                return getPageName();
            }
            if (getCommuneWaySelected().equals("---请选择---")) {
                String message = "请选择沟通方式！";
                getDelegate().record(message, ValidationConstraint.CONSISTENCY);
                return getPageName();
            }
            if (getCommuneResult().equals("---请选择---")) {
                String message = "请选择一个沟通结果！";
                getDelegate().record(message, ValidationConstraint.CONSISTENCY);
                return getPageName();
            }

            Contact c = getContactInfo();
            Process process = new Process();
            process.setSerialNum(getSerialNum());
            process.setCommuneResult(getCommuneResult());
            process.setCommuneWay(getCommuneWaySelected());
            process.setConclusion(getConclusionSelected());
            process.setCustomer(c.getCustomer());
            process.setContactId(c.getId());
            process.setUser(getUserSession());
            process.setIsHandle("0");

            String currentDate = Tools.formatDate(getCommuneTime(), Tools.DATEANDTIME);
            process.setCommuneTime(currentDate);

            Integer processDays = translate(getConclusionSelected());
            process.setStepNum(processDays);

            if (processDays != 0) {
                process.setRemindDate(Tools.getRemindInfo(processDays));
            } else {
                process.setRemindDate("N");
            }

            String commentString = getComment() == null ? "none" : getComment();
            process.setComment(commentString);

            getProcessService().create(process);

            String message = "新的沟通信息已经成功保存!";
            getDelegate().record(message, ValidationConstraint.CONSISTENCY);

            log.info(getUserSession().getUsername() + " : Save process success! \n" + process.toString());

            ProcessInfo p = getProcessInfo();
            p.setContactInfo(c);
            return p.getPageName();
        }
        catch (Exception e) {
            e.printStackTrace();
            String message = "保存数据时发生错误！";
            getDelegate().record(message, ValidationConstraint.CONSISTENCY);
            return getPageName();
        }
    }

    public void pageBeginRender(PageEvent event) {
        if (null == getUserSession() || null == getContactInfo()) {
            event.getRequestCycle().sendRedirect("app?page=SysPageError&service=page");
        }
    }

}